/* Copyright (C) 2008 Nick Crafford <nickcrafford@earthlink.net>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

            var board;
            var iGG; //Could be a gamePiece or a gameSlot
            var sGG; //Could be a gamePiece or a gameSlot

            var loadingScreen;
            var loadingScreenLag = 5; //5 Seconds

            var boardImage   = 'gfx/board2.gif';

            var nXSlots      = 10;
            var nYSlots      = 10;
            var slotHeight   = 50;
            var slotWidth    = 50;
            var bHeight      = nYSlots * slotHeight;
            var bWidth       = nXSlots * slotWidth;
            var boardXCenter = bWidth  / 2;
            var boardYCenter = bHeight / 2;
            var offsetX      = Math.ceil((window.innerWidth  / 2)  - boardXCenter);
            var offsetY      = Math.ceil((window.innerHeight / 2)  - boardYCenter);

            var cpuLag       = 1000;
            var clickOpac    = .6;

            //Build Game Screen
            function startGame()
            {
                loadingScreen = new gameAlert(offsetX,offsetY,bWidth,bHeight,'black')
                setupBoard();
                loadingScreen.show('<font style="font-size:36px;">Stratego</font>' +
				   '<br><br>'+
				   '<ul>'+
				     '<li>Silent Defense</li>'+
				     '<li>Aggressor Advantage</li>' +
				     '<li>Human Player: Cyclone Defense</li>' +
				     '<li>Computer Player: Random</li> ' +
				   '</ul>'); //Replace this with an image :)
                loadingScreen.close(loadingScreenLag*1000); //Close the loading screen after 5 seconds
            }

            function setupBoard()
            {
                board   = new gameBoard(offsetX,offsetY,nXSlots,nYSlots,slotHeight,slotWidth, '', boardImage);
		board.attachSound('sound/bg.wav');
                board.paint();
                board.setBoardCycloneDefense(false, 1, 'red' , false, false);
                board.randomSetup(true,  2, 'blue', true,  false);
                //board.setBoardCycloneDefense(true,  2, 'blue', true,  true);

                board.setIllegalSlot(2,4);
                board.setIllegalSlot(3,4);
                board.setIllegalSlot(2,5);
                board.setIllegalSlot(3,5);
                board.setIllegalSlot(6,4);
                board.setIllegalSlot(7,4);
                board.setIllegalSlot(6,5);
                board.setIllegalSlot(7,5);
            }

            function boardclicked(id)
            {
                resetState = false;

                //Nothing has been clicked yet
                if(iGG == undefined) //A piece or slot hasn't been clicked yet
                {
                    iGG = board.getPieceOrSlot(id);

                    if(iGG.type == 'piece' && !iGG.cpu)
                    {
                        board.attachSound('sound/pop.wav');
                        iGG.changeOpacity(clickOpac);
                    }


                }
                else  //A piece or slot has been clicked
                {
                    sGG        = board.getPieceOrSlot(id);
                    resetState = true;
                }

                if(!iGG.cpu)
                {
                    //Add a piece to the board
                    //iGG is defined
                    //sGG is not defined
                    //iGG is a slot
                    if(iGG != undefined && sGG == undefined && iGG.type == 'slot')
                    {
                        resetState = true;
                    }
                    else if(iGG != undefined && sGG != undefined && iGG.type == 'piece' && iGG.playerNum != board.lockedPlayer)
                    {
                        board.attachSound('sound/pop.wav');

                        if(board.moveGP(iGG,sGG.x,sGG.y))
                        {
                            board.lockedPlayer = iGG.playerNum;
                            r = setTimeout('board.randomMove(2)', cpuLag);
                        }
                    }

                }

                if(resetState)
                {
                    board.setAllPiecesOpac();
                    iGG = undefined;
                    sGG = undefined;
                }
            }

            function boarddblclicked(id)
            {

            }

            function boardMoused(id)
            {
                var gg = board.getSlot(id);
                var x  = gg.x;
                var y  = gg.y;

                window.status = 'x,y : ' + x +','+y;
            }
